package cn.itcast.myapplication.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import cn.itcast.myapplication.R;

public class DetailFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_detail, container, false);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Button button;
        //获取按钮进行跳转
        button = getView().findViewById(R.id.detail_btn);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //当你想切换Fragment时，使用NavController对象，告诉它你想要去Navigation Graph中的哪个Fragment，
                // NavController会将你想去的Fragment展示在NavHostFragment中。
                NavController controller = Navigation.findNavController(view);//导航控制器

                controller.navigate(R.id.action_detailFragment_to_homeFragment2);

            }

        });


        //在Navigation布局文件中设置好参数在这里把他替换掉（这个适合替换掉一些静态的数据）
        String my_name = getArguments().getString("my_name");//获取自变量

        TextView textView = getView().findViewById(R.id.detail_text);

        textView.setText(my_name);


    }


}